Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can_rate? can not deal with dimension = nil #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brookzhang
Copy link

If dimension.nil? , query should be " dimension is null ", not "dimension = '' ".

@@ -42,7 +42,10 @@ def average(dimension=nil)
end

def can_rate?(user_id, dimension=nil)
val = self.connection.select_value("select count(*) as cnt from rates where rateable_id=#{self.id} and rateable_type='#{self.class.name}' and rater_id=#{user_id} and dimension='#{dimension}'").to_i
query = "select count(*) as cnt from rates where rateable_id=#{self.id} and rateable_type='#{self.class.name}' and rater_id=#{user_id} "
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both the old version and this one don't work with inheritance because the polymorphic association will use the class.parent_name instead of class.name

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a better approach would be let active record help us, something like:

  val = rates.select_value("count(*) as cnt").where(dimension: dimension, rater_id: user_id)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is much simpler and will work find with model inheritance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants